#include "imagestatusmonitor.h"
#include "ui_imagestatusmonitor.h"

#include <QDialog>
#include <Qlist>
#include <QFile>

#include "toolutils.h"

namespace {
    const QStringList COMBOBOX_ZOOM_LIST {"10%", "20%", "30%", "40%", "50%", "60%", "70%", "80%", "90%", "100%", "110%", "120%", "150%", "200%", "300%", "500%", "800%"};
    constexpr int KB = 1024;
    constexpr int MB = KB * 1024;
}

ImageStatusMonitor::ImageStatusMonitor(ImageEditor* imageEditor, QWidget *parent)
    : QWidget(parent)
    , mImageEditor(imageEditor)
    , ui(new Ui::ImageStatusMonitor)
{
    setAttribute(Qt::WA_DeleteOnClose);
    ui->setupUi(this);
    Init();
    ConnectAll();
}

ImageStatusMonitor::~ImageStatusMonitor()
{
    delete ui;
}

void ImageStatusMonitor::Init()
{
    this->setStyleSheet(ToolUtils::ReadStyleSheet(":/qss/MointorQButton.qss"));
    mImageEditor->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    mImageEditor->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->comboBoxZoomScale->addItems(COMBOBOX_ZOOM_LIST);
    UpdateResolutionLabel();
    UpdateSizeLabel();
    Zoom(mScale);
}

void ImageStatusMonitor::UpdateResolutionLabel()
{
    auto imageSize = mImageEditor->Pixmap().size();
    ui->labelResolutionLabel->setText(QString::asprintf("分辨率 %dx%d", imageSize.width(), imageSize.height()));
}

void ImageStatusMonitor::UpdateSizeLabel()
{
    auto file = QFile(mImageEditor->ImagePath());
    qint64 fileSize = 0;
    if (file.open(QIODevice::ReadOnly)) {
        fileSize = file.size();
    }
    QString format;
    if (fileSize < KB) {
        format = "大小 %1B";
    } else if (fileSize >= KB && fileSize < MB) {
        format = "大小 %1KB";
        fileSize /= KB;
    } else {
        format = "大小 %1MB";
        fileSize /= MB;
    }
    ui->labelFileSize->setText(format.arg(fileSize));
}

void ImageStatusMonitor::ConnectAll()
{
    connect(ui->buttonZoomRestore, &QPushButton::clicked, this, &ImageStatusMonitor::ZoomRestore);
    connect(ui->buttonZoomSmaller, &QPushButton::clicked, this, &ImageStatusMonitor::ZoomSmaller);
    connect(ui->buttonZoomBigger, &QPushButton::clicked, this, &ImageStatusMonitor::ZoomBigger);
    connect(ui->buttonFullScreen, &QPushButton::clicked, this, &ImageStatusMonitor::FullScreen);
    // slider & comboBox
    connect(ui->sliderZoomScale, &QSlider::valueChanged, this, &ImageStatusMonitor::SliderZoomScaleChanged);
    connect(ui->comboBoxZoomScale, &QComboBox::currentIndexChanged, this, &ImageStatusMonitor::BoxZoomScaleChanged);
}


void ImageStatusMonitor::ZoomRestore()
{
    qDebug("ImageStatusMonitor::ZoomRestore");
    Zoom(1.);
}

void ImageStatusMonitor::ZoomSmaller()
{
    qDebug("ImageStatusMonitor::ZoomSmaller");
    Zoom(mScale - 0.1 > 0.1 ? mScale - 0.1 : mScale);
}

void ImageStatusMonitor::ZoomBigger()
{
    qDebug("ImageStatusMonitor::ZoomBigger");
    Zoom(mScale + 0.1 < 8 ? mScale + 0.1 : mScale);
}

void ImageStatusMonitor::FullScreen()
{
    qDebug("ImageStatusMonitor::FullScreen");
    auto dialog = new QDialog(this);
    auto layout = new QHBoxLayout(dialog);
    auto imageEditor = new ImageEditor(mImageEditor->ImagePath(), dialog);

    // connect, 关闭全屏后回收dialog的内存
    connect(dialog, &QDialog::finished, dialog, [dialog]{dialog->deleteLater();});

    layout->addWidget(imageEditor);
    // Qt::Window 指定一个窗口是一个独立的顶级窗口
    // Qt::FramelessWindowHint 设置窗口为无边框和标题栏
    dialog->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
    dialog->showFullScreen();
}

void ImageStatusMonitor::SliderZoomScaleChanged(const int value)
{
    Zoom(static_cast<double> (value) / 100);
}

void ImageStatusMonitor::BoxZoomScaleChanged(const int index)
{
    Q_UNUSED(index);
    Zoom(ui->comboBoxZoomScale->currentText().split('%').first().toDouble() / 100);
}

void ImageStatusMonitor::Zoom(const double zoomFactor)
{
    mScale = zoomFactor;
    mImageEditor->setZoomFactor(mScale);

    auto scene = mImageEditor->scene();
    auto rect = scene->sceneRect();
    mImageEditor->fitInView(mImageEditor->scene()->sceneRect(), Qt::IgnoreAspectRatio);

    // comboBox 和 slider 实时更新
    auto value = static_cast<int>(zoomFactor * 100);
    ui->comboBoxZoomScale->setEditText(QString::asprintf("%d%%", value));
    ui->sliderZoomScale->setValue(value);
}

